﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

/*
 * Author:Conis
 * Date:
 * Description:条件块的基类，组合模式中的最顶层类
 */

namespace Conis.Squirrel
{
    /// <summary>
    /// 条件块的基类
    /// </summary>
    public abstract class ConditionUnit
    {
        protected List<ConditionUnit> list;
        protected StringBuilder container;
        protected List<IDataParameter> paramsList;
        protected int currentIndex;
        public Connector Connector;

        public ConditionUnit()
        {
            this.list = new List<ConditionUnit>();
        }
        /// <summary>
        /// 循环之后
        /// </summary>
        protected virtual void LoopAfter() { }
        /// <summary>
        /// 循环之前
        /// </summary>
        protected virtual void LoopBefore() { }

        /// <summary>
        /// 创建SQL语句
        /// </summary>
        protected virtual void Create()
        {
            this.LoopBefore();
            int index = 0;
            foreach (ConditionUnit item in list)
            {
                item.container = this.container;
                item.paramsList = this.paramsList;
                item.currentIndex = index;
                item.Create();
                index++;
            }
            this.LoopAfter();
        }

        /// <summary>
        /// 添加一个部件
        /// </summary>
        /// <param name="unit"></param>
        protected void Add(ConditionUnit unit)
        {
            this.list.Add(unit);
        }

        /// <summary>
        /// 创建一个参数
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        protected abstract IDataParameter CreateParameter(string key, object value);
    }
}
